package com.airline.service;

import com.airline.dao.FlightStockDAO;
import com.airline.entity.FlightStock;

public class FlightStockService {
    private final FlightStockDAO flightStockDAO = new FlightStockDAO();

    /**
     * 查询航班余票
     */
    public FlightStock getStockByFlightNo(String flightNo) {
        return flightStockDAO.findByFlightNo(flightNo);
    }

    /**
     * 减余票（订票时调用）
     */
    public boolean decreaseStock(String flightNo) {
        FlightStock stock = getStockByFlightNo(flightNo);
        if (stock == null) {
            System.out.println("航班余票记录不存在！");
            return false;
        }
        if (stock.getAvailableSeats() <= 0) {
            return false; // 余票不足
        }
        flightStockDAO.updateAvailableSeats(flightNo, -1); // 余票减1
        return true;
    }

    /**
     * 加余票（退票时调用）
     */
    public boolean increaseStock(String flightNo) {
        FlightStock stock = getStockByFlightNo(flightNo);
        if (stock == null) {
            System.out.println("航班余票记录不存在！");
            return false;
        }
        if (stock.getAvailableSeats() >= stock.getTotalSeats()) {
            return false; // 余票已达最大值
        }
        flightStockDAO.updateAvailableSeats(flightNo, 1); // 余票加1
        return true;
    }
}